﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc @Copy Right 2019
// 文件： GovBlackListIPRepository.cs
// 项目名称：斑马车务
// 创建时间：2019-01-09
// 负责人：lsc
// ===================================================================
using System;
using System.Text;
using System.Collections.Generic;
using CGT.Infrastructure.Entity;
using Dapper.Extensions;
using Dapper;

namespace CGT.Infrastructure.Repository
{
    /// <summary>
    ///IP黑名单 仓储层
    /// </summary>	
    public class GovBlackListIPRepository : BaseRepository<GovBlackListIP>
    {

        #region 构造

        public GovBlackListIPRepository() : base() { }

        public GovBlackListIPRepository(string connectionString) : base(connectionString) { }

        public GovBlackListIPRepository(DatabaseContext dbContext) : base(dbContext) { }

        #endregion

        /// <summary>
        /// 判断是否是黑名单
        /// </summary>
        /// <param name="ip"></param>
        /// <returns>如果true，则是黑名单</returns>
        public GovBlackListIP GetByIP(string ip)
        {
            string sql = "SELECT * FROM GovBlackListIP WITH(NOLOCK) WHERE IP=@IP";
            return FirstOrDefault(sql, new { IP = ip });
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="sql">查询语句</param>
        /// <param name="param">参数</param>
        /// <returns></returns>
        public Page<GovBlackListIP> GetPages(long pageIndex, long pageSize, string ip)
        {
            StringBuilder sql = new StringBuilder("SELECT * FROM GovBlackListIP WITH(NOLOCK)");
           
            if (!string.IsNullOrEmpty(ip))
            {
                DynamicParameters dparams = new DynamicParameters();
                sql.Append(" WHERE IP=@IP");
                dparams.Add("@IP", ip, System.Data.DbType.String);
                return Pages(pageIndex, pageSize, sql.ToString(), dparams);
            }
            else
            {
                return Pages(pageIndex, pageSize, sql.ToString());
            }

        }

    }
}